package string_question.leecode.test125;

/**
 * 验证回文串
 *
 * @author code_yc
 * @version 1.0
 * @date 2020/7/5 11:31
 */
public class Solution1 {

    public boolean isPalindrome(String s) {
        if (s == null || s.length() < 2) return true;
        String cs = s.toLowerCase();
        StringBuilder temp = new StringBuilder();
        for (char c : cs.toCharArray()) {
            if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ) {
                temp.append(c);
            }
        }
        return getAns(temp.toString().toCharArray());
    }

    private boolean getAns(char[] chars) {
        int n = chars.length;
        if (n < 2) return true;
        int l = 0;
        int r = n - 1;
        while (l < r) {
            if (chars[l++] != chars[r--]) {
                return false;
            }
        }
        return true;
    }

}
